package com.ujs.leetcode.TwoSum;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangshihao
 * @create 2023-10-15 21:03
 */
public class TwoSum {
    public static void main(String[] args) {
        int[] nums = new int[]{3, 3};
        int target = 6;
        int[] ints = twoSum(nums, target);
        System.out.println(Arrays.toString(ints));
    }

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        int[] res = new int[2];
        for (int i = 0; i < nums.length; i++) {
            int temp = target - nums[i];
            if (map.containsKey(temp) && map.get(temp) != i) {
                res[0] = map.get(temp);
                res[1] = i;
                return res;
            }
            map.put(nums[i], i);
        }
        return res;
    }
}
